﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using AMS.UI.Forms;
using AMS.DataAccess.Trademark.Interfaces;
using AMS.Trademark.Management.Properties;
using AMS.Common;
using AMS.ComponentModel;

namespace AMS.Trademark.Management
{
    /// <summary>
    /// 收文列表对话框
    /// </summary>
    public partial class RecvFileListDlg : BaseForm
    {
        /// <summary>
        /// 收文列表对话框
        /// </summary>
        public RecvFileListDlg()
        {
            InitializeComponent();
            cmbSource.Items.Add("所有");
            cmbSource.Items.Add("官方收文");
            cmbSource.Items.Add("客户收文");
            cmbSource.SelectedIndex = 0;
            grid.GridView.CellDoubleClick += new DataGridViewCellEventHandler(GridView_CellDoubleClick);
            ApplyDelayLoadData();
        }


        DataTable allRF;
        DataRow selecteRow;
        /// <summary>
        /// 收文号
        /// </summary>
        public string RFIndex
        {
            get { return selecteRow == null ? null : (string)selecteRow["RFIndex"]; }
        }
        /// <summary>
        /// 收文名称
        /// </summary>
        public string RFName
        {
            get { return selecteRow == null ? null : (string)selecteRow["RFName"]; }
        }

        /// <summary>
        /// 检测是否存在的回调
        /// </summary>
        public event ArgumentEventHandler<bool> CheckRFExists;
        /// <summary>
        /// 检测是否存在
        /// </summary>
        /// <param name="rfIndex"></param>
        /// <returns></returns>
        private bool OnCheckRFExists(string rfIndex)
        {
            if (CheckRFExists != null)
            {
                ArgumentEventArgs<bool> args = new ArgumentEventArgs<bool>(rfIndex);
                CheckRFExists(this, args);
                return args.ArgumentValue;
            }

            return false;
        }
        /// <summary>
        /// 
        /// </summary>
        protected override void DelayLoadData()
        {
            grid.Init("RFRepository");
            DoDataBind();
        }

        private void DoDataBind()
        {
            try
            {
                Cursor = Cursors.WaitCursor;
                Application.DoEvents();

                IRecvFileManager rfManager = ObjectUtility.GetObject<IRecvFileManager>("RFManager");
                allRF = rfManager.GetAllCategoryRecvFiles();
                DataTable filteredData = ApplyFilter();

                grid.DataBind(filteredData);
                grid.RecordCount = filteredData.Rows.Count;
            }
            finally
            {
                Cursor = Cursors.Default;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        private DataTable ApplyFilter()
        {
            string filter = null;
            if (!string.IsNullOrEmpty(txtKeyword.Text))
            {
                filter = string.Format("RFIndex like '%{0}%' OR RFName like '%{0}%'", txtKeyword.Text);
                if (cmbSource.SelectedIndex > 0)
                {
                    filter += " AND Source='" + GetSource() + "'";
                }

                allRF.DefaultView.RowFilter = filter;
                return allRF.DefaultView.ToTable();
            }
            else if (cmbSource.SelectedIndex > 0)
            {
                filter = "Source='" + GetSource() + "'";
                allRF.DefaultView.RowFilter = filter;
                return allRF.DefaultView.ToTable();
            }
            else
            {
                return allRF;
            }
        }

        private string GetSource()
        {
            if (cmbSource.SelectedIndex == 1)
            {
                return "O";
            }
            else if (cmbSource.SelectedIndex == 2)
            {
                return "C";
            }
            return "";
        }

        private void btnCanel_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            if (grid.GridView.Rows.Count == 0)
            {
                DialogResult = System.Windows.Forms.DialogResult.Cancel;
                Close();
                return;
            }

            if (grid.SelectedRow == null)
            {
                MessageBox.Show(Resources.SelectRFInList);
                DialogResult = System.Windows.Forms.DialogResult.None;
                return;
            }

            int index = grid.GetSelectedRowDataIndex();
            selecteRow = ((DataTable)grid.DataSource).Rows[index];
            string rfIndex = (string)selecteRow["RFIndex"];
            if (OnCheckRFExists(rfIndex))
            {
                MessageBox.Show(Resources.RFExistsInList);
                return;
            }

            DialogResult = System.Windows.Forms.DialogResult.OK;
        }

        void GridView_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            btnOK_Click(btnOK, EventArgs.Empty);
        }

        private void btnFilter_Click(object sender, EventArgs e)
        {
            DoDataBind();
        }

        private void txtKeyword_TextChanged(object sender, EventArgs e)
        {
            DoDataBind();
        }

        private void txtKeyword_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                new App.DelayLoading(this, 10, new MethodInvoker(delegate()
                    {
                        txtKeyword.Focus();
                    })).Execute();
            }
            else if (e.KeyCode == Keys.Down)
            {
                grid.GridView.Focus();
                SendKeys.Send("{DOWN}");
            }
            else if (e.KeyCode == Keys.Up)
            {
                grid.GridView.Focus();
                SendKeys.Send("{UP}");
            }
        }
    }
}
